import { Link, useSearchParams } from "react-router"
import { TestChapterEntity, TestEntity, TestPaperEntity } from "../../../../queries/test"


type RenderLinkProps = {
  chapter: TestChapterEntity,
  item: TestPaperEntity,
  href: string
}
const RenderLink = ({
  chapter,
  item,
  href
}: RenderLinkProps) => {
  if (chapter.is_lock && !item.is_free) {
    return (
      <div>
        已锁定，请联系管理员开通
      </div>
    )
  }
  return (
    <Link to={href}>
      已开放测试
    </Link>
  )
}
interface PaperListProps {
  chapter: TestChapterEntity,
  baseHref: string,
  test?: TestEntity
}
const PaperList = ({
  chapter,
  baseHref,
  test
}: PaperListProps) => {
  const [searchParams] = useSearchParams()
  return (
    <div>
      {chapter.papers?.map((item) => {
        let href = `${baseHref}/${item.id}/questions?${searchParams.toString()}`
        if(chapter.is_lock &&!item.is_free){
          href = "#"
        }
        return (
          <div
            className="bg-white px-10 py-1 mb-2 flex justify-between"
            key={item.id}
          >
            <div className="w-96">
              <Link to={href}>
                {test?.name}{chapter.name}
              </Link>
            </div>
            <div>
              <Link to={href}>
                {item.name},{item.time}分钟
              </Link>
            </div>
            <div>
              <RenderLink
                chapter={chapter}
                item={item}
                href={href}
              />
            </div>
          </div>
        )
      })}
      <div className="h-4 bg-white"></div>
    </div>
  )
}
export default PaperList